﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Web.Api.Server.Models;
using Web.Api.Server.ViewModels;
using Web.Api.Server.ViewModels.Shops;

namespace Web.Api.Server.Controllers.Shops
{
    [SwaggerTag("购物车接口")]
    [Route("api/shops/{shopId}/[controller]")]
    [ApiController]
    public class CartsController : ControllerBase
    {
        private readonly ApplicationDbContext _dbContext;

        public CartsController(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        [HttpGet]
        [ProducesResponseType(typeof(ResponseViewModel<IEnumerable<Cart>>), 200)]
        public async Task<IActionResult> Get(int shopId)
        {
            var carts = await _dbContext.Carts
                .Where(c => c.ShopId == shopId)
                .ToListAsync();
            return ApiResponse.Ok(carts);
        }

        [HttpPost]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Post(int shopId, CartsPostViewModel viewModel)
        {
            if (shopId == viewModel.ShopId)
            {
                var userId = 1;
                var cart = new Cart
                {
                    GoodsId = viewModel.GoodsId,
                    ShopId = viewModel.ShopId,
                    Number = viewModel.Number,
                    Price = viewModel.Price,
                    UserId = userId,
                };
                await _dbContext.Carts.AddAsync(cart);
                var result = await _dbContext.SaveChangesAsync();

                return ApiResponse.Ok(result);
            }

            return ApiResponse.NotFound();
        }

        [HttpPut("{id}")]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Put(int shopId, int id, CartsPostViewModel viewModel)
        {
            if (shopId == viewModel.ShopId && id == viewModel.Id)
            {
                var cart = await _dbContext.Carts
                    .Where(c => c.Id == viewModel.Id)
                    .FirstAsync();
                if (cart != null)
                {
                    cart.GoodsId = viewModel.GoodsId;
                    cart.ShopId = viewModel.ShopId;
                    cart.Number = viewModel.Number;
                    cart.Price = viewModel.Price;
                    cart.UpdatedAt = DateTime.Now;

                    await _dbContext.Carts.UpdateAsync(cart);
                    var result = await _dbContext.SaveChangesAsync();
                    return ApiResponse.Ok(result);
                }
            }

            return ApiResponse.NotFound();
        }

        [HttpDelete("{id}")]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Delete(int shopId, int id)
        {
            var cart = await _dbContext.Carts
                .Where(c => c.ShopId == shopId && c.Id == id)
                .FirstAsync();
            if (cart != null)
            {
                _dbContext.Carts.Remove(cart);
                var result = await _dbContext.SaveChangesAsync();
                return ApiResponse.Ok(result);
            }

            return ApiResponse.NotFound();
        }
    }
}